const router = require("koa-router")();
const { key } = require("../config/default");
const jwt = require("koa-jwt")({ secret: key });
const { insertDataToOrderList } = require("../utils/index");
const Mock = require("mockjs");
let { list } = Mock.mock({
  "list|500": [
    {
      id: "@id",
      start: "@city",
      end: "@city",
      "price|100-1000": 1,
      startTime: "@date",
      endTime: "@date",
      state: 1,
    },
  ],
});
// 下单接口（权限）
router.post("/create/order", jwt, async (ctx, next) => {
  ctx.verifyParams({
    price: { type: "number", required: true },
    title: { type: "string", required: true },
    start: { type: "string", required: true },
    end: { type: "string", required: true },
  });
  const { id: user_id } = ctx.state.user;
  const {
    price,
    title,
    start,
    end,
    beginTime = new Date().toLocaleString(),
    endTime = new Date(
      new Date().getTime() + 1000 * 60 * 60 * 4
    ).toLocaleString(),
  } = ctx.request.body;

  try {
    const resp = await insertDataToOrderList({
      user_id,
      price,
      title,
      start,
      end,
      beginTime,
      endTime,
    });

    if (resp.affectedRows) {
      ctx.body = {
        code: 200,
        message: "下单成功",
        data: {
          id: resp.insertId,
          price,
          title,
          start,
          end,
          beginTime,
          endTime,
          user_id,
        },
      };
    } else {
      ctx.body = {
        code: 400,
        message: "下单失败",
        error: resp,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: "下单失败",
      error: error,
    };
  }
});

router.get("/order/list", async (ctx, next) => {
  ctx.body = {
    code: 200,
    message: "查询成功",
    data: list,
  };
});

router.get("/order/list/detail", async (ctx, next) => {
  const { id } = ctx.request.query;
  const item = list.find((v) => v.id === id);
  if (item) {
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: item,
    };
  } else {
    ctx.body = {
      code: 400,
      message: "查询失败",
    };
  }
});

router.post("/order/list/edit", async (ctx, next) => {
  ctx.verifyParams({
    id: { type: "string", required: true },
    start: { type: "string", required: true },
    end: { type: "string", required: true },
  });

  const { id, start, end, ...args } = ctx.request.body;

  const item = list.find((v) => v.id === id);
  if (item) {
    Object.assign(item, { start, end, ...args });
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: item,
    };
  } else {
    ctx.body = {
      code: 400,
      message: "查询失败",
    };
  }
});

// // 查询订单
// router.get("/order/list", jwt, async (ctx, next) => {
//   ctx.verifyParams({
//     price: { type: "number", required: true },
//     title: { type: "string", required: true },
//     start: { type: "string", required: true },
//     end: { type: "string", required: true },
//   });
//   const { id: user_id } = ctx.state.user;
//   const {
//     price,
//     title,
//     start,
//     end,
//     beginTime = new Date().toLocaleString(),
//     endTime = new Date(new Date().getTime() + 1000 * 60 * 60 * 4).toLocaleString(),
//   } = ctx.request.body;

//   try {
//     const resp = await insertDataToOrderList({
//       user_id,
//       price,
//       title,
//       start,
//       end,
//       beginTime,
//       endTime,
//     });

//     if (resp.affectedRows) {
//       ctx.body = {
//         code: 200,
//         message: "下单成功",
//         data: {
//           id: resp.insertId,
//           price,
//           title,
//           start,
//           end,
//           beginTime,
//           endTime,
//           user_id,
//         },
//       };
//     } else {
//       ctx.body = {
//         code: 400,
//         message: "下单失败",
//         error: resp,
//       };
//     }
//   } catch (error) {
//     ctx.body = {
//       code: 500,
//       message: "下单失败",
//       error: error,
//     };
//   }
// });

// 获取订单列表接口
router.get("/", jwt, async (ctx, next) => {});

module.exports = router;
